#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2016-2018, Niklas Hauser
# Copyright (c) 2017, Fabian Greif
#
# This file is part of the modm project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# -----------------------------------------------------------------------------

def get_properties(env):
    device = env[":target"]
    driver = device.get_driver("spi:avr_uart")
    properties = device.properties
    properties["target"] = device.identifier
    properties["driver"] = driver
    return properties


class Instance(Module):
    def __init__(self, instance):
        self.instance = instance

    def init(self, module):
        module.name = str(self.instance)
        module.description = "Instance {}".format(self.instance)

    def prepare(self, module, options):
        module.depends(":platform:spi_uart")

        module.add_option(
            BooleanOption(
                name="buffered",
                description="",
                default=True))
        module.add_option(
            NumericOption(
                name="buffer.tx",
                description="",
                minimum=1, maximum=254,
                default=64))
        module.add_option(
            NumericOption(
                name="buffer.rx",
                description="",
                minimum=1, maximum=254,
                default=8))

        return True

    def build(self, env):
        properties = get_properties(env)
        properties["id"] = self.instance

        target = properties["target"]

        properties["extended"] = False
        if target["name"] in ["48", "88", "168", "328"] and target["type"] == "p":
            properties["extended"] = True
        if target["name"] in ["64", "128", "256"] and target["type"] in ["rfr1", "rfr2"]:
            properties["extended"] = True

        if target["family"] == "tiny" and target["name"] in ["828", "2313", "4313"]:
            instance = ""
        else:
            instance = self.instance
        properties["id"] = instance

        env.substitutions = properties
        env.outbasepath = "modm/src/modm/platform/spi"

        if instance == "":
            env.template("uart_spi_master.hpp.in")
            env.template("uart_spi_master.cpp.in")
        else:
            env.template("uart_spi_master.hpp.in", "uart_spi_master_{}.hpp".format(self.instance))
            env.template("uart_spi_master.cpp.in", "uart_spi_master_{}.cpp".format(self.instance))


def init(module):
    module.name = ":platform:uart.spi"
    module.description = "USART in SPI Mode"

def prepare(module, options):
    device = options[":target"]
    if not device.has_driver("spi:avr_uart"):
        return False

    module.depends(
        ":architecture:atomic",
        ":architecture:spi",
        ":math:utils",
        ":platform:gpio",
        ":platform:uart")

    for instance in listify(device.get_driver("spi:avr_uart")["instance"]):
        module.add_submodule(Instance(int(instance)))

    return True

def build(env):
    env.substitutions = get_properties(env)
    env.outbasepath = "modm/src/modm/platform/spi_uart"
